﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace HgRemote
{
	public partial class SettingsDialog : Form
	{
		public SettingsDialog()
		{
			InitializeComponent();
		}

		private void SettingsDialog_Load(object sender, EventArgs e)
		{
			Settings s = Settings.Current;
			if (s.User != null)
			{
				FullName.Text = s.User.Name;
				Email.Text = s.User.Email;
			}
		}

		private void SaveBtn_Click(object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(FullName.Text))
				MessageBox.Show(this, "Full Name is required.");
			else if (String.IsNullOrEmpty(Email.Text))
				MessageBox.Show(this, "Email is required.");
			else if(!Regex.IsMatch(Email.Text, @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", RegexOptions.IgnoreCase))
				MessageBox.Show(this, "Invalid email address.");
			else
			{
				Settings s = Settings.Current;
				s.User = new HgRemoteContracts.HgUser { Name = FullName.Text, Email = Email.Text };
				s.SaveChanges();

				this.DialogResult = DialogResult.OK;
				this.Close();
			}
		}

		private void CancelBtn_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}
	}
}
